﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder.Service
{
    public class CreateBaseService
    {
        private string tableName;
        private string fileDir;
        public CreateBaseService(string tableName, string fileDir)
        {
            this.tableName = tableName;
            this.fileDir = fileDir;
        }

        public void Run()
        {
            var formatTableName = Helper.GetPascalCase(tableName).Substring(1);
            var dtoName = $"{formatTableName}Dto";
            var privateDtoName = Helper.GetCamelCase(dtoName);
            var entityName = $"{Helper.GetPascalCase(tableName)}Entity";
            var privateEntityName = Helper.GetCamelCase(entityName);
            var iRepository = $"I{Helper.GetPascalCase(tableName)}Repository ";
            var privateIReporty = Helper.GetCamelCase(iRepository.Substring(1));
            var fileName = $"BaseService";
            IList<string> contents = new List<string>();
            contents.Add("using Hualong.TestWorldPro.Repository;");
            contents.Add("using Microsoft.Extensions.Logging;");
            contents.Add("");
            contents.Add("namespace Hualong.TestWorldPro.Services");
            contents.Add("{");
            contents.Add("    public class BaseService: IBaseService");
            contents.Add("    {");
            contents.Add("        public readonly IBaseRepository baseRepository;");
            contents.Add("        public readonly ILogger<BaseService> logger;");
            contents.Add("");
            contents.Add("        public BaseService(IBaseRepository baseRepository, ILogger<BaseService> logger)");
            contents.Add("        {");
            contents.Add("            this.baseRepository = baseRepository;");
            contents.Add("            this.logger = logger;");
            contents.Add("        }");
            contents.Add("    }");
            contents.Add("}");
            string filePath = Path.Combine(fileDir, fileName);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{fileName}.cs");
            if (File.Exists(filePath))
            { File.Delete(filePath); }
            File.WriteAllLines(filePath, contents, Encoding.UTF8);

            Console.WriteLine($"创建:{filePath} 完成！");
        }
    }
}
